package org.deng.learn.mybatis.xml;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.deng.learn.mybatis.entity.User;
import org.deng.learn.mybatis.xml.mapper.UserMapper;


import java.util.List;

/**
 * @Author DengLibin
 * @Date 19-4-27
 * @Time 下午6:50
 * @Desc User表 数据库访问
 */
public class UserDao {

    private SqlSessionFactory sessionFactory = SingletonFactory.getSqlSessionFactory();

    /**
     * 使用SqlSession 发送sql语句(不易阅读)
     * @return
     */
    public List<User> query(){
        SqlSession sqlSession = sessionFactory.openSession();
        try{
            List<User> list = sqlSession.selectList("org.deng.learn.mybatis.xml.mapper.UserMapper.query");
            sqlSession.commit();
            return list;
        }finally {
            sqlSession.close();
        }
    }

    /**
     * 使用mapper发送sql语句 (易阅读)
     * @return
     */
    public List<User> query2(){
        SqlSession sqlSession = sessionFactory.openSession();
        try{
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            List<User> list = userMapper.query();
            sqlSession.commit();
            return list;
        }finally {
            sqlSession.close();
        }
    }
}
